home *** CD-ROM | disk | FTP | other *** search
/ Quick PC 61 / Quick PC 61.iso / I386 / NETFX.CAB / WebUIValidation.js < prev    next >
Encoding:
Text File  |  2003-02-21  |  14.1 KB  |  402 lines

  1. var Page_ValidationVer = "125";
  2. var Page_IsValid = true;
  3. var Page_BlockSubmit = false;
  4. function ValidatorUpdateDisplay(val) {
  5.     if (typeof(val.display) == "string") {    
  6.         if (val.display == "None") {
  7.             return;
  8.         }
  9.         if (val.display == "Dynamic") {
  10.             val.style.display = val.isvalid ? "none" : "inline";
  11.             return;
  12.         }
  13.     }
  14.     val.style.visibility = val.isvalid ? "hidden" : "visible";
  15. }
  16. function ValidatorUpdateIsValid() {
  17.     var i;
  18.     for (i = 0; i < Page_Validators.length; i++) {
  19.         if (!Page_Validators[i].isvalid) {
  20.             Page_IsValid = false;
  21.             return;
  22.         }
  23.    }
  24.    Page_IsValid = true;
  25. }
  26. function ValidatorHookupControlID(controlID, val) {
  27.     if (typeof(controlID) != "string") {
  28.         return;
  29.     }
  30.     var ctrl = document.all[controlID];
  31.     if (typeof(ctrl) != "undefined") {
  32.         ValidatorHookupControl(ctrl, val);
  33.     }
  34.     else {
  35.         val.isvalid = true;
  36.         val.enabled = false;
  37.     }
  38. }
  39. function ValidatorHookupControl(control, val) {
  40.     if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number") {
  41.         var i;
  42.         for (i = 0; i < control.length; i++) {
  43.             var inner = control[i];
  44.             if (typeof(inner.value) == "string") {
  45.                 ValidatorHookupControl(inner, val);
  46.             } 
  47.         }
  48.         return;
  49.     }
  50.     else if (control.tagName != "INPUT" && control.tagName != "TEXTAREA" && control.tagName != "SELECT") {
  51.         var i;
  52.         for (i = 0; i < control.children.length; i++) {
  53.             ValidatorHookupControl(control.children[i], val);
  54.         }
  55.         return;
  56.     }
  57.     else {
  58.         if (typeof(control.Validators) == "undefined") {
  59.             control.Validators = new Array;
  60.             var ev;
  61.             if (control.type == "radio") {
  62.                 ev = control.onclick;
  63.             } else {
  64.                 ev = control.onchange;
  65.             }
  66.             if (typeof(ev) == "function" ) {            
  67.                 ev = ev.toString();
  68.                 ev = ev.substring(ev.indexOf("{") + 1, ev.lastIndexOf("}"));
  69.             }
  70.             else {
  71.                 ev = "";
  72.             }
  73.             var func = new Function("ValidatorOnChange(); " + ev);
  74.             if (control.type == "radio") {
  75.                 control.onclick = func;
  76.             } else {            
  77.                 control.onchange = func;
  78.             }
  79.         }
  80.         control.Validators[control.Validators.length] = val;
  81.     }    
  82. }
  83. function ValidatorGetValue(id) {
  84.     var control;
  85.     control = document.all[id];
  86.     if (typeof(control.value) == "string") {
  87.         return control.value;
  88.     }
  89.     if (typeof(control.tagName) == "undefined" && typeof(control.length) == "number") {
  90.         var j;
  91.         for (j=0; j < control.length; j++) {
  92.             var inner = control[j];
  93.             if (typeof(inner.value) == "string" && (inner.type != "radio" || inner.status == true)) {
  94.                 return inner.value;
  95.             }
  96.         }
  97.     }
  98.     else {
  99.         return ValidatorGetValueRecursive(control);
  100.     }
  101.     return "";
  102. }
  103. function ValidatorGetValueRecursive(control)
  104. {
  105.     if (typeof(control.value) == "string" && (control.type != "radio" || control.status == true)) {
  106.         return control.value;
  107.     }
  108.     var i, val;
  109.     for (i = 0; i<control.children.length; i++) {
  110.         val = ValidatorGetValueRecursive(control.children[i]);
  111.         if (val != "") return val;
  112.     }
  113.     return "";
  114. }
  115. function Page_ClientValidate() {
  116.     var i;
  117.     for (i = 0; i < Page_Validators.length; i++) {
  118.         ValidatorValidate(Page_Validators[i]);
  119.     }
  120.     ValidatorUpdateIsValid();    
  121.     ValidationSummaryOnSubmit();
  122.     Page_BlockSubmit = !Page_IsValid;
  123.     return Page_IsValid;
  124. }
  125. function ValidatorCommonOnSubmit() {
  126.     event.returnValue = !Page_BlockSubmit;
  127.     Page_BlockSubmit = false;
  128. }
  129. function ValidatorEnable(val, enable) {
  130.     val.enabled = (enable != false);
  131.     ValidatorValidate(val);
  132.     ValidatorUpdateIsValid();
  133. }
  134. function ValidatorOnChange() {
  135.     var vals = event.srcElement.Validators;
  136.     var i;
  137.     for (i = 0; i < vals.length; i++) {
  138.         ValidatorValidate(vals[i]);
  139.     }
  140.     ValidatorUpdateIsValid();    
  141. }
  142. function ValidatorValidate(val) {    
  143.     val.isvalid = true;
  144.     if (val.enabled != false) {
  145.         if (typeof(val.evaluationfunction) == "function") {
  146.             val.isvalid = val.evaluationfunction(val); 
  147.         }
  148.     }
  149.     ValidatorUpdateDisplay(val);
  150. }
  151. function ValidatorOnLoad() {
  152.     if (typeof(Page_Validators) == "undefined")
  153.         return;
  154.     var i, val;
  155.     for (i = 0; i < Page_Validators.length; i++) {
  156.         val = Page_Validators[i];
  157.         if (typeof(val.evaluationfunction) == "string") {
  158.             eval("val.evaluationfunction = " + val.evaluationfunction + ";");
  159.         }
  160.         if (typeof(val.isvalid) == "string") {
  161.             if (val.isvalid == "False") {
  162.                 val.isvalid = false;                                
  163.                 Page_IsValid = false;
  164.             } 
  165.             else {
  166.                 val.isvalid = true;
  167.             }
  168.         } else {
  169.             val.isvalid = true;
  170.         }
  171.         if (typeof(val.enabled) == "string") {
  172.             val.enabled = (val.enabled != "False");
  173.         }
  174.         ValidatorHookupControlID(val.controltovalidate, val);
  175.         ValidatorHookupControlID(val.controlhookup, val);
  176.     }
  177.     Page_ValidationActive = true;
  178. }
  179. function ValidatorConvert(op, dataType, val) {
  180.     function GetFullYear(year) {
  181.         return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
  182.     }
  183.     var num, cleanInput, m, exp;
  184.     if (dataType == "Integer") {
  185.         exp = /^\s*[-\+]?\d+\s*$/;
  186.         if (op.match(exp) == null) 
  187.             return null;
  188.         num = parseInt(op, 10);
  189.         return (isNaN(num) ? null : num);
  190.     }
  191.     else if(dataType == "Double") {
  192.         exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + val.decimalchar + "(\\d+))?\\s*$");
  193.         m = op.match(exp);
  194.         if (m == null)
  195.             return null;
  196.         cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
  197.         num = parseFloat(cleanInput);
  198.         return (isNaN(num) ? null : num);            
  199.     } 
  200.     else if (dataType == "Currency") {
  201.         exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + val.groupchar + ")*)(\\d+)"
  202.                         + ((val.digits > 0) ? "(\\" + val.decimalchar + "(\\d{1," + val.digits + "}))?" : "")
  203.                         + "\\s*$");
  204.         m = op.match(exp);
  205.         if (m == null)
  206.             return null;
  207.         var intermed = m[2] + m[5] ;
  208.         cleanInput = m[1] + intermed.replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
  209.         num = parseFloat(cleanInput);
  210.         return (isNaN(num) ? null : num);            
  211.     }
  212.     else if (dataType == "Date") {
  213.         var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-./])(\\d{1,2})\\4(\\d{1,2})\\s*$");
  214.         m = op.match(yearFirstExp);
  215.         var day, month, year;
  216.         if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
  217.             day = m[6];
  218.             month = m[5];
  219.             year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
  220.         }
  221.         else {
  222.             if (val.dateorder == "ymd"){
  223.                 return null;        
  224.             }                        
  225.             var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-./])(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
  226.             m = op.match(yearLastExp);
  227.             if (m == null) {
  228.                 return null;
  229.             }
  230.             if (val.dateorder == "mdy") {
  231.                 day = m[3];
  232.                 month = m[1];
  233.             }
  234.             else {
  235.                 day = m[1];
  236.                 month = m[3];
  237.             }
  238.             year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
  239.         }
  240.         month -= 1;
  241.         var date = new Date(year, month, day);
  242.         return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
  243.     }
  244.     else {
  245.         return op.toString();
  246.     }
  247. }
  248. function ValidatorCompare(operand1, operand2, operator, val) {
  249.     var dataType = val.type;
  250.     var op1, op2;
  251.     if ((op1 = ValidatorConvert(operand1, dataType, val)) == null)
  252.         return false;    
  253.     if (operator == "DataTypeCheck")
  254.         return true;
  255.     if ((op2 = ValidatorConvert(operand2, dataType, val)) == null)
  256.         return true;
  257.     switch (operator) {
  258.         case "NotEqual":
  259.             return (op1 != op2);
  260.         case "GreaterThan":
  261.             return (op1 > op2);
  262.         case "GreaterThanEqual":
  263.             return (op1 >= op2);
  264.         case "LessThan":
  265.             return (op1 < op2);
  266.         case "LessThanEqual":
  267.             return (op1 <= op2);
  268.         default:
  269.             return (op1 == op2);            
  270.     }
  271. }
  272. function CompareValidatorEvaluateIsValid(val) {
  273.     var value = ValidatorGetValue(val.controltovalidate);
  274.     if (ValidatorTrim(value).length == 0)
  275.         return true;
  276.     var compareTo = "";
  277.     if (null == document.all[val.controltocompare]) {
  278.         if (typeof(val.valuetocompare) == "string") {
  279.             compareTo = val.valuetocompare;
  280.         }
  281.     }
  282.     else {
  283.         compareTo = ValidatorGetValue(val.controltocompare);
  284.     }
  285.     return ValidatorCompare(value, compareTo, val.operator, val);
  286. }
  287. function CustomValidatorEvaluateIsValid(val) {
  288.     var value = "";
  289.     if (typeof(val.controltovalidate) == "string") {
  290.         value = ValidatorGetValue(val.controltovalidate);
  291.         if (ValidatorTrim(value).length == 0)
  292.             return true;
  293.     }
  294.     var args = { Value:value, IsValid:true };
  295.     if (typeof(val.clientvalidationfunction) == "string") {
  296.         eval(val.clientvalidationfunction + "(val, args) ;");
  297.     }        
  298.     return args.IsValid;
  299. }
  300. function RegularExpressionValidatorEvaluateIsValid(val) {
  301.     var value = ValidatorGetValue(val.controltovalidate);
  302.     if (ValidatorTrim(value).length == 0)
  303.         return true;        
  304.     var rx = new RegExp(val.validationexpression);
  305.     var matches = rx.exec(value);
  306.     return (matches != null && value == matches[0]);
  307. }
  308. function ValidatorTrim(s) {
  309.     var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
  310.     return (m == null) ? "" : m[1];
  311. }
  312. function RequiredFieldValidatorEvaluateIsValid(val) {
  313.     return (ValidatorTrim(ValidatorGetValue(val.controltovalidate)) != ValidatorTrim(val.initialvalue))
  314. }
  315. function RangeValidatorEvaluateIsValid(val) {
  316.     var value = ValidatorGetValue(val.controltovalidate);
  317.     if (ValidatorTrim(value).length == 0) 
  318.         return true;
  319.     return (ValidatorCompare(value, val.minimumvalue, "GreaterThanEqual", val) &&
  320.             ValidatorCompare(value, val.maximumvalue, "LessThanEqual", val));
  321. }
  322. function ValidationSummaryOnSubmit() {
  323.     if (typeof(Page_ValidationSummaries) == "undefined") 
  324.         return;
  325.     var summary, sums, s;
  326.     for (sums = 0; sums < Page_ValidationSummaries.length; sums++) {
  327.         summary = Page_ValidationSummaries[sums];
  328.         summary.style.display = "none";
  329.         if (!Page_IsValid) {
  330.             if (summary.showsummary != "False") {
  331.                 summary.style.display = "";
  332.                 if (typeof(summary.displaymode) != "string") {
  333.                     summary.displaymode = "BulletList";
  334.                 }
  335.                 switch (summary.displaymode) {
  336.                     case "List":
  337.                         headerSep = "<br>";
  338.                         first = "";
  339.                         pre = "";
  340.                         post = "<br>";
  341.                         final = "";
  342.                         break;
  343.                     case "BulletList":
  344.                     default: 
  345.                         headerSep = "";
  346.                         first = "<ul>";
  347.                         pre = "<li>";
  348.                         post = "</li>";
  349.                         final = "</ul>";
  350.                         break;
  351.                     case "SingleParagraph":
  352.                         headerSep = " ";
  353.                         first = "";
  354.                         pre = "";
  355.                         post = " ";
  356.                         final = "<br>";
  357.                         break;
  358.                 }
  359.                 s = "";
  360.                 if (typeof(summary.headertext) == "string") {
  361.                     s += summary.headertext + headerSep;
  362.                 }
  363.                 s += first;
  364.                 for (i=0; i<Page_Validators.length; i++) {
  365.                     if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
  366.                         s += pre + Page_Validators[i].errormessage + post;
  367.                     }
  368.                 }   
  369.                 s += final;
  370.                 summary.innerHTML = s; 
  371.                 window.scrollTo(0,0);
  372.             }
  373.             if (summary.showmessagebox == "True") {
  374.                 s = "";
  375.                 if (typeof(summary.headertext) == "string") {
  376.                     s += summary.headertext + "<BR>";
  377.                 }
  378.                 for (i=0; i<Page_Validators.length; i++) {
  379.                     if (!Page_Validators[i].isvalid && typeof(Page_Validators[i].errormessage) == "string") {
  380.                         switch (summary.displaymode) {
  381.                             case "List":
  382.                                 s += Page_Validators[i].errormessage + "<BR>";
  383.                                 break;
  384.                             case "BulletList":
  385.                             default: 
  386.                                 s += "  - " + Page_Validators[i].errormessage + "<BR>";
  387.                                 break;
  388.                             case "SingleParagraph":
  389.                                 s += Page_Validators[i].errormessage + " ";
  390.                                 break;
  391.                         }
  392.                     }
  393.                 }
  394.                 span = document.createElement("SPAN");
  395.                 span.innerHTML = s;
  396.                 s = span.innerText;
  397.                 alert(s);
  398.             }
  399.         }
  400.     }
  401. }
  402.